<template>
  <div>
    {{ ids }}
    <el-form :inline="true" :model="pageData" class="demo-form-inline">
      <div class="good">
        <el-form-item label="规格名称">
          <el-input
            v-model="pageData.specName"
            placeholder="规格名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="list">查询</el-button>
          <el-button type="primary" @click="onAdd">添加</el-button>
          <el-button type="primary" @click="ondelById">删除</el-button>
        </el-form-item>
      </div>
    </el-form>
    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
      ref="multipleTable"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="id" label="ID"> </el-table-column>
      <el-table-column prop="specName" label="规格名称"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageData.pageNum"
      :page-sizes="[3, 5, 7, 10]"
      :page-size="pageData.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog :title="opsTitle" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="活动名称" :label-width="formLabelWidth">
          <el-input v-model="form.specName" autocomplete="off"></el-input>
        </el-form-item>

        <el-button @click="onspecation">新增规格选项</el-button>

        <el-table :data="form.specificationOptionList" style="width: 100%">
          <el-table-column label="规格选项">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.optionName"
                placeholder="请输入内容"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column label="排序">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.orders"
                placeholder="请输入内容"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column label="选择">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAndupdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  spcList,
  saveOps,
  findByIds,
  updateById,
  removeById,
  delById,
} from "@/api/spcList";
export default {
  data() {
    return {
      tableData: [],
      pageData: {
        pageNum: 1,
        pageSize: 5,
        specName: "",
      },
      total: 0,
      ids: [],
      form: {
        id: null,
        specName: "",
        specificationOptionList: [],
      },
      dialogFormVisible: false,
      formLabelWidth: "120px",
      opsTitle: "",
    };
  },
  created() {
    this.list();
  },
  methods: {
    list() {
      spcList(this.pageData).then((response) => {
        this.tableData = response.data.data.list;
        this.total = response.data.data.total;
      });
    },
    //条数
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageData.pageSize = val;
      this.list();
    },
    //页数
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageData.pageNum = val;
      this.list();
    },
    //修改
    handleEdit(index, row) {
      console.log(index, row);
      findByIds(row.id).then((response) => {
        this.form = response.data.data;
        this.dialogFormVisible = true;
      });
    },
    //删除
    handleDelete(index, row) {
      delById(row.id).then((result) => {
        this.list();
      });
    },
    //显示弹框
    onAdd() {
      this.form.id = null;
      this.form.specName = "";
      this.form.specificationOptionList = [];
      this.opsTitle = "添加";

      this.dialogFormVisible = true;
    },
    //新增规格
    onspecation() {
      this.form.specificationOptionList.push({});
    },
    saveAndupdate() {
      if (this.form.id == null) {
        //添加规格和详情信息
        saveOps(this.form).then((response) => {
          if (response.data.code == 200) {
            this.$message.success("添加成功");
            this.form.id = null;
            this.form.specName = "";
            this.form.specificationOptionList = [];
            this.dialogFormVisible = false;
            this.list();
          }
          //
        });
      } else {
        //修改规格和详情
        updateById(this.form).then((response) => {
          if (response.data.code == 200) {
            this.$message.success("修改成功");
            this.form.id = null;
            this.form.specName = "";
            this.form.specificationOptionList = [];
            this.dialogFormVisible = false;
            this.list();
          }
        });
      }
    },
    //全选/反选
    handleSelectionChange(val) {
      this.ids = [];
      if (val.length > 0) {
        val.forEach((row) => {
          this.ids.push(row.id);
        });
      } else {
        this.ids = [];
      }
    },
    //删除数据
    ondelById() {
      removeById(this.ids).then((response) => {
        if (response.data.code == 2000) {
          this.$message.success("删除成功");
          this.list();
        } else {
          this.$message.error(response.data.msg);
        }
      });
    },
  },
};
</script>

<style scoped>
/* .good {
  margin-left: 600px;
} */
</style>
